package cn.tedu.charging.order.rabbit;

import cn.tedu.charging.order.config.RabbitConfiguration;
import cn.tedu.charging.order.pojo.po.OrderMQPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: Miyako
 * @CreateTime: 2024-10-21  00:35
 * @Description: 生产者
 */
@Slf4j
@Component
public class RabbitMQProducer {

    @Autowired
    private RabbitTemplate rabbitTemplate;



    public void sendOrder(OrderMQPO orderMQPO){
        //告警机制？
        rabbitTemplate.setReturnsCallback(returnedMessage -> {
            log.debug("returnedMessage{}",returnedMessage);
        });
        //设置 return 回调类 并回执
        rabbitTemplate.setConfirmCallback((data,ack,cause)->{
            log.debug("data:{},ack:{},cause:{}",data,ack,cause);
            if (ack){
                log.debug("RabbitMQ成功收到了消息");
            }else {
                log.debug("rabbitmq拒绝了消息，原因是{}",cause);
            }
        });

        rabbitTemplate.convertAndSend(RabbitConfiguration.EXCHANGE_NAME,RabbitConfiguration.ROUTING_KEY,orderMQPO);
    }
}
